package base.items;

import java.awt.Image;
import java.util.HashSet;

import javax.swing.ImageIcon;

/**
 * The Photo {@code class}. Create Photo object extended from an Item.
 * 
 * @author Peter NGO, Guillaume POUSSEL and Mathieu RUL
 * 
 * @version 1.0
 */
public class Photo extends Item {

	/**
	 * Location of the Photo.
	 */
	private String location;

	/**
	 * Width of the Photo.
	 */
	private int width;

	/**
	 * Height of the Photo.
	 */
	private int height;

	/**
	 * Constructor of the {@code class}. Create a Photo from an ID.
	 * 
	 * @param ID
	 *            the Photo ID.
	 */
	public Photo(Integer ID) {
		super(ID);
		setType("Photo");
	}

	/**
	 * Constructor of the {@code class}. Create a Photo from parameter.
	 * 
	 * @param ID
	 *            the Photo ID.
	 * 
	 * @param title
	 *            the Photo title.
	 * 
	 * @param tags
	 *            the Photo tags.
	 * 
	 * @param comments
	 *            the Photo comments.
	 * 
	 * @param location
	 *            the Photo location.
	 * 
	 * @param width
	 *            the Photo width.
	 * 
	 * @param height
	 *            the Photo height.
	 */
	public Photo(Integer ID, String title, HashSet<Integer> tags,
			HashSet<Integer> comments, String location, int width, int height) {
		super(ID, "Photo", title, tags, comments);

		this.location = location;
		this.width = width;
		this.height = height;
	}

	/**
	 * Returns a {@code String} array with the allowed MIME types for a Photo.
	 * 
	 * @return a string array with the allowed MIME.
	 */
	@Override
	public String[] allowedMimeTypes() {
		return new String[] { "image/gif", "image/jpeg", "image/png",
				"image/tiff" };
	}

	/**
	 * Get the height of the Photo.
	 * 
	 * @return the height.
	 */
	public int getHeight() {
		return height;
	}

	/**
	 * Get the location of the Photo.
	 * 
	 * @return the location.
	 */
	public String getLocation() {
		return location;
	}

	/**
	 * Get the width of the Photo.
	 * 
	 * @return the width.
	 */
	public int getWidth() {
		return width;
	}

	/**
	 * Parse photo data for extracting the photo's size.
	 * 
	 * @return {@code true} if the sizes were correctly extracted; {@code false}
	 *         otherwise.
	 */
	public boolean parseData() {
		if (!super.parseData())
			return false;
		try {
			Image img = new ImageIcon(getPath()).getImage();
			setWidth(img.getWidth(null));
			setHeight(img.getHeight(null));
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	/**
	 * Set the height of the Photo.
	 * 
	 * @param height
	 *            the height to set.
	 */
	public void setHeight(int height) {
		this.height = height;
	}

	/**
	 * Set the location of the Photo.
	 * 
	 * @param location
	 *            the location to set.
	 */
	public void setLocation(String location) {
		this.location = location;
	}

	/**
	 * Set the width of the Photo.
	 * 
	 * @param width
	 *            the width to set.
	 */
	public void setWidth(int width) {
		this.width = width;
	}

	/**
	 * Returns a {@code String} object representing the Photo's value.
	 * 
	 * @return a string representation of the Photo.
	 */
	@Override
	public String toString() {
		return super.toString() + "\nLocation: " + location + "\nResolution: "
				+ width + "x" + height;
	}
}
